/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.boundary;

import giuliani_mapelli_marocco.business.EventManager;
import giuliani_mapelli_marocco.business.MessageManager;
import giuliani_mapelli_marocco.business.TimeTools;
import giuliani_mapelli_marocco.business.UserManager;
import giuliani_mapelli_marocco.business.WeatherManager;
import giuliani_mapelli_marocco.entity.Event;
import giuliani_mapelli_marocco.entity.Weatherforecast;
import giuliani_mapelli_marocco.enumerations.WeatherType;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named
@ViewScoped
public class WeatherBean implements Serializable{
    
    @EJB
    EventManager em;
    
    @EJB 
    UserManager um;
    
    @EJB
    MessageManager mm;
    
    @EJB
    WeatherManager wm;
    
    private Event event;
    private List<Weatherforecast> weatherForecastList;
    private Map<Weatherforecast, String> weatherStringMap;

    /**
     * Creates a new instance of WeatherBean
     */
    public WeatherBean() {
        this.weatherStringMap = new HashMap<>();
    }
    
    /**
     * Initializes the bean using the "idEvent" parameter.
     */
    @PostConstruct
    public void init() {
        FacesContext context = FacesContext.getCurrentInstance();
        String idEvent = context.getExternalContext()
                    .getRequestParameterMap().get("idEvent");
        if(idEvent != null) {
            event = em.findEventById( Long.valueOf(idEvent));
            weatherForecastList = (List<Weatherforecast>) wm.getWeatherByIdEvent(event.getId());
            populateWeatherStringMap();
        }
    }
    
    public List<Weatherforecast> getWeatherForecastList() {
        return weatherForecastList;
    }
    
    public String getWeatherString(Weatherforecast wf) {
        return weatherStringMap.get(wf);
    }
    
    /**
     * Given a weatherforecast, it returns its date formatted
     * @param wf weatherforecast
     * @return the formatted date string
     */
    public String getFormattedDate(Weatherforecast wf) {
        Date dateToConvert = wf.getWeatherforecastPK().getDate();
        String formattedDate = TimeTools.convertDateToString(dateToConvert);
        return formattedDate;
    }
    
    /**
     * for every weather forecast it get the description of the weather
     */
    private void populateWeatherStringMap() {
        for(Weatherforecast w: weatherForecastList) {
            for(WeatherType wt :WeatherType.values()) {
                if(wt.getCode() == w.getWeather()) {
                    weatherStringMap.put(w, wt.getDescription());
                    break;
                }
            }
        }
    
    }
}